﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Device.Location;
using System.Linq;
using System.Net;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;qq
using System.Windows.Navigation;
using HappyTravelling.DataModel;
using HappyTravelling.ThanhService;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Controls.Maps.Platform;
using Microsoft.Phone.Shell;
using HappyTravelling.Resources;
using Newtonsoft.Json;
using Telerik.Windows.Controls;

namespace HappyTravelling.Views
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        private ApplicationBar addBar, nextBar;
        private ApplicationBarIconButton bt1, bt2, bt3, bt4;
        private Pivot mainPivot;
        public static bool isOpenListInvite = false;
        public MainPage()
        {
            InitializeComponent();
            CreateApplicationBarButtons();
            mpContent.EditEvent += EditMeeting;
            mpContent.ViewEvent += ViewMeeting;
        }

        private void ViewMeeting(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/MeetingDetail.xaml", UriKind.Relative));
        }

        private void EditMeeting(object sender, EventArgs e)
        {
           
        }


        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            mpContent.GetData();
        }

        private void CreateApplicationBarButtons()
        {
            addBar = new ApplicationBar();
            bt1 = new ApplicationBarIconButton
            {
                Text = "Tạo sự kiện",
                IconUri = new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative)
            };
            bt1.Click += new EventHandler(btnAdd_Click);
            bt1.IsEnabled = true;
            addBar.Buttons.Add(bt1);
            bt2 = new ApplicationBarIconButton
            {
                Text = "Thư mời",
                IconUri = new Uri("/Assets/AppBar/appbar.photo.redeye.rest.png", UriKind.Relative)
            };
            bt2.Click += new EventHandler(btnGetInvite_Click);
            bt2.IsEnabled = true;
            addBar.Buttons.Add(bt2);
            ApplicationBar = addBar;
            ApplicationBar.Opacity = 0.5;
        }

        private void btnGetInvite_Click(object sender, EventArgs e)
        {
            if (!isOpenListInvite)
            {
                GetAllInvitation();
                isOpenListInvite = true;
            }
            else
            {
                mpContent.GetData();
                isOpenListInvite = false;
            }
            
        }

        public void GetAllInvitation()
        {
            try
            {
                WebService1SoapClient client = new WebService1SoapClient();
                client.GetMyInvitationRequestAsync(App.CurrentUser.id);
                client.GetMyInvitationRequestCompleted += ClientOnGetMyInvitationRequestCompleted;
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        public void ClientOnGetMyInvitationRequestCompleted(object sender, GetMyInvitationRequestCompletedEventArgs e)
        {
            try
            {
                string json = e.Result;
                List<Invitation> myRequests = JsonConvert.DeserializeObject<List<Invitation>>(json);
                List<DisplayListMeeting> ListMeetingInvitation = new List<DisplayListMeeting>();
                foreach (var invitation in myRequests)
                {
                    ListMeetingInvitation.Add(new DisplayListMeeting
                    {
                        ID = invitation.MeetingEvent.Id,
                        Location = new GeoCoordinate(invitation.MeetingEvent.Latitude, invitation.MeetingEvent.Longtitude),
                        TenAdmin = invitation.MeetingEvent.Leader.DisplayName,
                        TenCuocHen = invitation.MeetingEvent.Content,
                        Time = invitation.MeetingEvent.Time.ToShortDateString()
                    });
                }
                mpContent.ListMeetingBox.ItemsSource = ListMeetingInvitation;
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }


        private void btnAdd_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/AddDating.xaml", UriKind.Relative));
        }

        private void ListFriendsToAdd_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void rwdAbout_WindowOpening(object sender, CancelEventArgs e)
        {
            try
            {
                WebService1SoapClient client = new WebService1SoapClient();
                //client.CreateUserCompleted += client_CreateUserCompleted;
                NavigationService.GoBack();
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

    }

    public class DisplayListMeeting //: INotifyPropertyChanged
    {
        public int ID { get; set; }
        public String TenCuocHen { get; set; }
        public String TenAdmin { get; set; }
        public string Time { get; set; }
        public GeoCoordinate Location { get; set; }

        //public event PropertyChangedEventHandler PropertyChanged;
        //private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        //{
        //    if (PropertyChanged != null)
        //    {
        //        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        //    }
        //}
    }
}